<template>
  <el-tabs v-model="activeName">
    <el-tab-pane label="车辆调拨信息" name="first">
      <FormPanel ref="formPanel" :list="formList" :labelPosition="'top'" :formType="type" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" :cols="4" />
      <FormPanel ref="formPanel1" :list="formList1" :labelPosition="'top'" :formType="type" @formChange="changeHandle1" :isSubmitBtn="false" :collapse="false" labelWidth="110" :cols="4" />
      <FormPanel ref="formPanel2" :list="formList2" :labelPosition="'top'" :formType="type" @formChange="changeHandle2" :isSubmitBtn="false" :collapse="false" labelWidth="110" :cols="4" />
      <FormPanel ref="formPanel4" :list="formList4" :labelPosition="'top'" :formType="type" :isSubmitBtn="false" :collapse="false" labelWidth="110" :cols="4" />
    </el-tab-pane>
    <div style="height: 47px"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center',
      }"
    >
      <el-button type="primary" v-if="type === 'edit'" @click="updateAudit(1)">审核通过</el-button>
      <el-button type="primary" v-if="type === 'edit'" @click="updateAudit(2)">审核驳回</el-button>
      <el-button @click="closeDrawer">关 闭</el-button>
    </div>
  </el-tabs>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getInfoById, audit } from '@/api/omSaleP/VehicleAllocation/RegionalAudit';

export default {
  mixins: [dictionary],
  props: ['type', 'rowData'],

  data() {
    this.saveData = null;
    this.saveData1 = null;
    this.saveData2 = null;
    this.saveData4 = null;
    return {
      formList: this.createdFrom(),
      formList1: this.createdFrom1(),
      formList2: this.createdFrom2(),
      formList4: this.createdFrom4(),
      dataSource: [],
      isCan: false,
      isExamine: false,
      empList: [],
      carInfo: {},
      customerInfo: {},
      salesReportInfo: {},
      activeName: 'first',
    };
  },
  created() {
    this.formList.forEach((item) => {
      item.disabled = true;
    });
    this.formList1.forEach((item) => {
      item.disabled = true;
    });
    this.formList2.forEach((item) => {
      item.disabled = true;
    });
    if (this.type !== 'edit') {
      this.formList4.forEach((item) => {
        item.disabled = true;
      });
    }
  },
  mounted() {
    this.init();

      if (this.type !== 'edit') {
        this.formList.forEach((item) => {
          item.rules = [];
        });
        this.formList1.forEach((item) => {
          item.rules = [];
        });
        this.formList2.forEach((item) => {
          item.rules = [];
        });
      }

  },
  methods: {
    init() {
      if (this.type != 'add') {
        this.getDetail();
      }
    },

    async updateAudit(val) {
      let statusParams = {
        trNo: this.rowData.trNo,
        status: val,
      };
      const data = { ...this.$refs.formPanel4.SUBMIT_FORM() };
      // console.log(data.areaAuditOpinion);
      let errStatus = null;
      if (val === 2 && (!data || !data.areaAuditOpinion)) {
        this.isExamine = true;
        this.formList4 = this.createdFrom4();
        console.log(this.formList4);
        this.saveData4.areaAuditOpinion = data.areaAuditOpinion;
        let [err, data4] = await this.$refs.formPanel4.GET_FORM_DATA();
        console.log(err);
        errStatus = err;
      }
      statusParams.areaAuditOpinion = data.areaAuditOpinion;
      if (val === 2 && errStatus) return;
      console.log(val === 2, errStatus, val === 2 && errStatus);
      const res = await audit(statusParams);
      this.closeDrawer();
    },
    // 详细
    async getDetail() {
      console.log('rowData',this.rowData)
      const res = await getInfoById(this.rowData.trNo);
      if (res.resultCode == 200) {
        for (let i in res.data) {
          this.formList.map((x) => {
            if (x.fieldName == i && res.data[i]) {
              x.initialValue = res.data[i];
            }
          });
          this.formList1.map((x) => {
            if (x.fieldName == i) {
              x.initialValue = res.data[i];
            }
          });
          this.formList2.map((x) => {
            if (x.fieldName == i) {
              x.initialValue = res.data[i];
            }
          });
          this.formList4.map((x) => {
            if (x.fieldName == i) {
              x.initialValue = res.data[i];
            }
          });
        }
      }
    },
    changeHandle(val) {
      this.saveData = val;
    },
    changeHandle1(val) {
      this.saveData1 = val;
    },
    changeHandle2(val) {
      this.saveData2 = val;
    },
    closeDrawer(type) {
      this.$emit('close', false, type);
    },


    // 翻译省市区
    transDictText(val, dirtArr) {
      console.log(dirtArr, '---翻译---');
      let res = '';
      if (!val) return res;
      if (Array.isArray(dirtArr)) {
        const target = dirtArr.find((x) => x.value == val);
        res = target ? target.text || '' : val;
      }
      return res;
    },
    // ---------------------------调入店信息---------------------------
    createdFrom() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '调入店信息',
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'transferInName',
          initialValue: '',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '地址',
          fieldName: 'addressIn',
          initialValue: '',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '联系人',
          fieldName: 'contactsIn',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '联系人手机号',
          fieldName: 'contactsNumberIn',
          placeholder: '',
          disabled: true,
        }
      ];
    },
    // ----------------------------调出店信息--------------------------
    createdFrom1() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '调出店信息',
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'transferOutName',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '地址',
          fieldName: 'addressOut',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '联系人',
          fieldName: 'contactsOut',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '联系人手机号',
          fieldName: 'contactsNumberOut',
          placeholder: '',
          disabled: true,
        }
      ];
    },
    // ----------------------------车辆信息--------------------------
    createdFrom2() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '车辆信息',
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '物料编码',
          fieldName: 'productCode',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '物料描述',
          fieldName: 'productName',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '车系',
          fieldName: 'seriesName',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelName',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '配置',
          fieldName: 'configName',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '选装',
          fieldName: 'optionName',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '内饰',
          fieldName: 'trimName',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '外饰',
          fieldName: 'colorName',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '库龄',
          fieldName: 'stockAge',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '市场指导价',
          fieldName: 'retailPrice',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remark',
          disabled: true,
          placeholder: '',
        }
      ];
    },
    // ----------------------------审核信息--------------------------
    createdFrom4() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '审核信息',
        },
        {
          type: 'TEXT_AREA',
          label: '区域审批意见',
          fieldName: 'areaAuditOpinion',
          maxlength: 120,
          placeholder: '',
          // rules: this.isExamine ? [{ required: true, message: '请输入区域审批意见', trigger: 'blur' }] : [],
          selfCols: 3,
        },
        {
          type: 'TEXT_AREA',
          label: '总部审批意见',
          fieldName: 'headquartersAuditOpinion',
          maxlength: 120,
          placeholder: '',
          // rules: this.isExamine ? [{ required: true, message: '请输入总部审批意见', trigger: 'blur' }] : [],
          selfCols: 3,
        }
      ];
    }
  },
};
</script>
